<!--
 * @Author: 邵朋 
 * @Date: 2023-12-08 19:19:11
 * @LastEditors: 邵朋 
 * @LastEditTime: 2024-01-13 14:00:30
 * @FilePath: /YouDian/src/App.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<script lang="ts">
import * as btViewmodel from '@/util/bluetoothUtil/btViewmodel'
	import Vue from 'vue';
	export default Vue.extend({
		mpType: 'app',
		data() {
			return {
				network: true, //默认有望
			}
		},
		onLaunch() {
			console.log('App Launch')
			// uni.onTabBarMidButtonTap(() => {
			// 	(this as any).$approuter.pushNative('/pages/takeOrder/index', false)

			// });
		},
		onShow() {
			console.log('App Show')
		},
		onHide() {
			console.log('App Hide')
		},
		mounted() {
           let self = this;
			uni.onBluetoothAdapterStateChange(function (res) {
				if(res.available === false){
					(self as any).$User.isConnectedBlueTooth = false;
					(self as any).$User.isTempConnectedBlueTooth = false;
					self.uploadBlueToothStatusData();

					(self as any).$toast.infoToast('蓝牙已断开连接app');

					(self as any).$User.currentConnectedEquipment.deviceID = '';
					(self as any).$User.currentConnectedBlueTooth = {} as any

					 ///超过20条就上报一次
					 console.log('设备断开，准备上传最后一组实时数据')
					 btViewmodel.uploadDeviceDataReq()
				}
			})


			uni.onBLEConnectionStateChange(function (res) {
				if(res.connected === false){				
					(self as any).$User.isConnectedBlueTooth = false;
					(self as any).$User.isTempConnectedBlueTooth = false;

					self.uploadBlueToothStatusData();
					(self as any).$User.currentConnectedBlueTooth.deviceId = null as any
					// (self as any).$toast.infoToast('蓝牙已断开连接app2');
					(self as any).$User.currentConnectedEquipment.deviceID = '';
					(self as any).$User.currentConnectedBlueTooth =  {} as any

					 ///超过20条就上报一次
					 console.log('设备断开，准备上传最后一组实时数据')
					 btViewmodel.uploadDeviceDataReq()
				}
				// 该方法回调中可以用于处理连接意外断开等异常情况
				console.log(`设备 ${res.deviceId} 连接状态改变: ${res.connected}`)
			})

			// // 检测断网
			// window.addEventListener("offline", () => {
			//     console.log("已断网");
			//     this.network= false;
			//     (this as any).$approuter.pushNative('/util/404/404')
			// });
			// window.addEventListener("online", () => {
			//     console.log("网络已连接");
			//     this.network= true;
			//     (this as any).$approuter.back();
			// });
		},
		methods:{
			///上报蓝牙连接状态
		uploadBlueToothStatusData(){

			let param = {'deviceid':(this as any).$User.currentConnectedEquipment.deviceID,
							'status':(this as any).$User.isConnectedBlueTooth ? '1' : '0',
			};
			(this as any).$api.reportBlueToothinfo(param).then((res:any)=>{

				// console.log('蓝牙状态上传参数：'+JSON.stringify(param))
				// console.log('蓝牙状态上传结果：'+JSON.stringify(res))

			})

		}
		}
	});
</script>

<style>
 

</style>